package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.Article;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 文章
 * TableName : ARTICLE
 * ClassName : com.swlz.dao.ArticleDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-02 11:20:40
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class ArticleDAO {

	/**
	 * 添加一个文章
	 * 包含所有字段
	 * @param article - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(Article article) throws SQLException {
		if(article.getArticleId()==null) {
			article.setArticleId(DBUtil.nextId());
		}
		article.setCreateTime(new Date());
		article.setVersionNum(0);
		article.setSortNumber(0L);
		return DBUtil.insert(article);
	}
 
	
	/**
	 * 添加一个文章
	 * 只包含非null的字段
	 * @param article - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(Article article) throws SQLException {
		if(article.getArticleId()==null) {
			article.setArticleId(DBUtil.nextId());
		}
		article.setCreateTime(new Date());
		article.setVersionNum(0);
		article.setSortNumber(0L);
		return DBUtil.insertSelective(article);
	}
	

	/**
	 * 根据主键更新一个文章
	 * 包含所有字段
	 * @param article - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(Article article) throws SQLException {
		article.setUpdateTime(new Date());
		return DBUtil.update(article);
	}
	
	/**
	 * 根据主键更新一个文章
	 * 只包含非null的字段
	 * @param article - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(Article article) throws SQLException {
		article.setUpdateTime(new Date());
		return DBUtil.updateSelective(article);
	}
	
	/**
	 * 根据主键删除一个文章
	 * @param articleId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long articleId) throws SQLException  {
		return DBUtil.execute("DELETE FROM ARTICLE WHERE ARTICLE_ID=?", articleId);
	}
	
	/**
	 * 根据主键获取一个文章
	 * @param articleId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Article selectById(Long articleId) throws SQLException  {
		return DBUtil.get("SELECT * FROM ARTICLE WHERE ARTICLE_ID=?", Article.class ,articleId);
	}
	
	/**
	 * 上一篇文章
	 * @param articleId
	 * @return
	 * @throws SQLException
	 */
	public Article selectPrevious(Long articleId)throws SQLException {
		return DBUtil.get("SELECT * from article WHERE ARTICLE_TYPE_ID= (SELECT ARTICLE_TYPE_ID FROM article where article_id=?) AND ARTICLE_TIME < (SELECT ARTICLE_TIME FROM article where article_id=?)  ORDER BY  ARTICLE_TIME desc LIMIT 1", Article.class ,articleId,articleId);
	}
	
	/**
	 * 下一篇文章
	 * @param articleId
	 * @return
	 * @throws SQLException
	 */
	public Article selectNext(Long articleId)throws SQLException {
		return DBUtil.get("SELECT * from article WHERE ARTICLE_TYPE_ID= (SELECT ARTICLE_TYPE_ID FROM article where article_id=?) AND ARTICLE_TIME > (SELECT ARTICLE_TIME FROM article where article_id=?)  ORDER BY  ARTICLE_TIME LIMIT 1", Article.class ,articleId,articleId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param articleId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Article selectByModel(Article article)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM ARTICLE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(article,sql,params);
		return DBUtil.get(sql.toString(), Article.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param articleId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<Article> selectByModel(Article article,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM ARTICLE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(article,sql,params);
		sql.append(" ORDER BY ARTICLE_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), Article.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param articleId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(Article article)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM ARTICLE WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(article,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param articleId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(Article article,StringBuffer sql,List<Object> params) {
		if(article==null){
			return;
		}

		if(article.getArticleId()!=null) {
			sql.append(" AND ARTICLE_ID=?");
			params.add(article.getArticleId());
		}

		if(article.getArticleTypeId()!=null) {
			sql.append(" AND ARTICLE_TYPE_ID=?");
			params.add(article.getArticleTypeId());
		}

		if(article.getArticleCode()!=null) {
			sql.append(" AND ARTICLE_CODE=?");
			params.add(article.getArticleCode());
		}

		if(article.getArticleTitle()!=null) {
			sql.append(" AND ARTICLE_TITLE=?");
			params.add(article.getArticleTitle());
		}

		if(article.getArticleWorker()!=null) {
			sql.append(" AND ARTICLE_WORKER=?");
			params.add(article.getArticleWorker());
		}

		if(article.getArticleContent()!=null) {
			sql.append(" AND ARTICLE_CONTENT=?");
			params.add(article.getArticleContent());
		}

		if(article.getArticleStatus()!=null) {
			sql.append(" AND ARTICLE_STATUS=?");
			params.add(article.getArticleStatus());
		}

		if(article.getArticleImgs()!=null) {
			sql.append(" AND ARTICLE_IMGS=?");
			params.add(article.getArticleImgs());
		}

		if(article.getArticleFiles()!=null) {
			sql.append(" AND ARTICLE_FILES=?");
			params.add(article.getArticleFiles());
		}

		if(article.getArticleTags()!=null) {
			sql.append(" AND ARTICLE_TAGS=?");
			params.add(article.getArticleTags());
		}

		if(article.getArticleTime()!=null) {
			sql.append(" AND ARTICLE_TIME=?");
			params.add(article.getArticleTime());
		}

		if(article.getDeleted()!=null) {
			sql.append(" AND DELETED=?");
			params.add(article.getDeleted());
		}

		if(article.getDeleteUser()!=null) {
			sql.append(" AND DELETE_USER=?");
			params.add(article.getDeleteUser());
		}

		if(article.getDeleteTime()!=null) {
			sql.append(" AND DELETE_TIME=?");
			params.add(article.getDeleteTime());
		}

		if(article.getCreateUser()!=null) {
			sql.append(" AND CREATE_USER=?");
			params.add(article.getCreateUser());
		}

		if(article.getCreateTime()!=null) {
			sql.append(" AND CREATE_TIME=?");
			params.add(article.getCreateTime());
		}

		if(article.getVersionNum()!=null) {
			sql.append(" AND VERSION_NUM=?");
			params.add(article.getVersionNum());
		}

		if(article.getUpdateUser()!=null) {
			sql.append(" AND UPDATE_USER=?");
			params.add(article.getUpdateUser());
		}

		if(article.getUpdateTime()!=null) {
			sql.append(" AND UPDATE_TIME=?");
			params.add(article.getUpdateTime());
		}

		if(article.getSortNumber()!=null) {
			sql.append(" AND SORT_NUMBER=?");
			params.add(article.getSortNumber());
		}

	}
	
	

}